<?php

/**
 * ReturnsController
 * 
 * @author
 * @version 
 */

class Merchant_ReturnsController extends Zend_Controller_Action {
	
	public function preDispatch(){
		$this->tplDirectory = "merchant/returns/";
	}
	
	public function indexAction() {
		
		echo Cff::renderTpl($this->tplDirectory."index.tpl","user-layout");
	}

	public function createAction() {		

		if($this->_request->isPost()){
			$customerService = new Merchant_Service_Customer();
			$customerInfo = $customerService->getLoginInfo()->customer;
			$customer_id = $customerInfo['customer_id'];
			
			$product_ids = $this->_request->product_id;	//产品ID 数组
			$op_quantitys = $this->_request->op_quantity;
			$description = $this->_request->instructions;	//特别说明
			$reference_no = $this->_request->getParam('reference_no','');	//Reference No
			
			$order_type = $this->_request->getParam('order_type',2);	// 退货单类型
			$orders_code_2 = $this->_request->getParam('orders_code_2','');	
			$receiving_id = $this->_request->getParam('receiving_id','');
			if($order_type == 2){	//退货方式为订单方式提交时，确保$reference_no为订单号
				$orders_code_2 && $reference_no = $orders_code_2;
			}
			
			$receivingArr = array();
			$receivingArr['customer_id'] = $customer_id;
			$receivingArr['warehouse_id'] = 1;  	//只有一个仓库，ID为 1
			$receivingArr['order_type'] = $order_type;		
			$receivingArr['description'] = $description;
			$receivingArr['ref_id'] = $reference_no;
			$receivingArr['status'] = 1; 					//1 等待状态
			$receivingArr['created'] = date('Y-m-d H:i:s');			
			$receivingArr['pickup'] = 0;		//暂时默认为0
			$receivingArr['supplier_id'] = '';		//供应商ID 暂时为空	
						
			foreach ($product_ids as $key => $val){
				$productArr[$val] = $op_quantitys[$key];
			}

			$data =array('ask'=>0,'receiving_code'=>'','msg'=>'');
			$obj = new Merchant_Service_RmaProccess();
			if($receiving_id){	//更新
				$tmpArr = $obj->update($productArr,$receivingArr,$receiving_id,'receiving_id');
			}else{	// 新建				
				$tmpArr = $obj->create($productArr,$receivingArr);	
			}
			if(count($tmpArr)){
				$data = $tmpArr;
			}

			die(json_encode($data));			
		}
	
	}
	
	public function listAction() {		
		$status = $this->_request->getParam('status',1);
		$this->view->status = $status;
		
		if($this->_request->isPost()){
			$customerService = new Merchant_Service_Customer();
			$customerInfo = $customerService->getLoginInfo()->customer;
			$customer_id = $customerInfo['customer_id'];
			if(!$customer_id)return false;
			
			//$orderType 收货类型（1 => 标准，2 =>订单退货；3：指定产品退件）
			$orderType = Merchant_Service_Receiving::$orderType;	
			$cond =array();
			$receiving_code = $this->_request->getParam('receiving_code','');
			$ref_id = $this->_request->getParam('ref_id','');			
			$dateFrom = $this->_request->getParam('dateFrom','');
			$dateTo = $this->_request->getParam('dateTo','');
			$status = $this->_request->getParam('status','');
			
			$this->view->status = $status;
			$receiving_code && $cond['receiving_code'] = $receiving_code;
			$ref_id && $cond['ref_id'] = $ref_id;
			$dateFrom && $cond['dateFrom'] = $dateFrom;
			$dateTo && $cond['dateTo'] = $dateTo;			
			if($status!==''){
				$cond['status'] = $status;
			}
			$cond['customer_id'] = $customer_id; 

			$page = $this->_request->getParam('page',1);
			$pageSize = $this->_request->getParam('pageSize',20);
			$page = $page?$page:1;
			$data =array('ask'=>0);			
			
			$tmpcond = $cond;
			//delete total
			$tmpcond['status'] = 0;
			$deleteTotal = Merchant_Service_Receiving::getRMAListByCond($tmpcond,'count(*)','receiving_id',0,0);
			
			//Awaiting total
			$tmpcond['status'] = 1;
			$awaitingTotal = Merchant_Service_Receiving::getRMAListByCond($tmpcond,'count(*)','receiving_id',0,0);
			
			//received total
			$tmpcond['status'] = 3;
			$receivedTotal = Merchant_Service_Receiving::getRMAListByCond($tmpcond,'count(*)','receiving_id',0,0);
			
			$data['statusTotal'] = array(
							"deleteTotal" => $deleteTotal,
							"awaitingTotal" => $awaitingTotal,
							"receivedTotal" => $receivedTotal  );
							
			$total = Merchant_Service_Receiving::getRMAListByCond($cond,'count(*)','receiving_id',0,0);
			$rows = Merchant_Service_Receiving::getRMAListByCond($cond,'*','receiving_id',$pageSize,($page-1)*$pageSize);
			if(!$total){
				die(json_encode($data));
			}
			$serviceObj = new Merchant_Service_Product();	
			
			$returnResult = Merchant_Service_RmaProccess::$returnResult;		
			foreach ($rows as $key =>$val){
				$products = array();
				$products = Merchant_Service_ReceivingItem::getAllByValue($val['receiving_code'],'receiving_code');

				$totalQty = $totalReceived = 0;
				foreach ($products as $k2 =>$v2){
					$productInfo = $serviceObj->getProductInfo($v2['product_id']);
					$products[$k2]['SKUNo'] = $productInfo['product_sku'];
					$products[$k2]['SKUName'] = $productInfo['product_title'];
					if($v2['remark']===NULL){
						$products[$k2]['remark'] = '';
					}
					$totalQty += $v2['order_qty'];			//退货申请总数
					$totalReceived += $v2['received_qty'];		//实际收货总数
				}
				
				$rows[$key]['returns_result'] = $returnResult[$customerInfo['return_package_proccess']];		//用户处理结果
				$rows[$key]['order_type'] = $orderType[$val['order_type']];
				$rows[$key]['products'] = $products;
				$rows[$key]['totalQty'] = $totalQty;
				$rows[$key]['received_qty'] = $totalReceived;
			}
		
			$data['ask'] = 1;
			$data['total'] = $total;
			$data['result'] = $rows;
			
			die(json_encode($data));
		}
				
		echo Cff::renderTpl($this->tplDirectory."list.tpl","user-layout");	
	}
	
	public function getProductsAction() {
		$orders_code = trim($this->_request->getParam('orders_code',''));
		$check = Merchant_Service_Orders::checkCustomerOrder($orders_code);
		
		$data = array('ask'=>0);		
		if($check){
			$data['msg'] = $check;
			die(json_encode($data));
		}
		
		$cond = array('orders_code'=>$orders_code);
		$rows = Merchant_Service_OrderProduct::getByCondition($cond);
		if(!$rows){
			$data['msg'] = 'No data,please check order code.';
			die(json_encode($data));
		}
		
		$productArr = array();
		$serviceObj = new Merchant_Service_Product();
		foreach ($rows as $key => $val){
			$productInfo = $serviceObj->getProductInfo($val['product_id']);
			if(count($productInfo)){
				$productInfo['order_qty'] = $val['op_quantity'];			
				$productInfo['product_status'] = $serviceObj->getStatusText($productInfo['product_status']);
				
				$productArr[] = $productInfo;
			}
		}
		if(count($productArr)){
			$data['ask'] = 1;
			$data['result'] = $productArr;
		}
		die(json_encode($data));		
	}
	
	
	public function editAction() {
		$receiving_id = $this->_request->getParam('id','');
		$this->view->receiving_id  = $receiving_id ;
		echo Cff::renderTpl($this->tplDirectory."edit.tpl","user-layout");
	}
	
	public function barcodeAction(){
		Merchant_Service_Barcode::barcode($this->_request->code);
	}
	
	public function deleteAction(){
		$receiving_code = $this->_request->getParam('id','');
		
		$data = array('ask'=>0);
		$row = array('status'=>0);
		//删除 receiving 更改status 为 0
		$receivingObj = new Merchant_Service_Receiving();
		$res = $receivingObj->update($row,$receiving_code,'receiving_code');

		if($res){
			$data['ask'] = 1;
		}
		die(json_encode($data));
		
	}
	
	
	/**
	 * 	检查 reference_no 唯一性，不存在返回true，
	 *  即当前reference_no 可以使用
	 */
	public function checkRefnoAction(){
		$reference_no = trim($this->_request->getParam('reference_no',''));
		if($reference_no != ''){
			$obj = new Merchant_Service_AsnProccess();
			$res = $obj->checkReferenceNo($reference_no);
			die(json_encode($res));
		}
	}
	
}
